from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
# 七牛云: 
    # 本地服务器存储
    # 云端服务器存储静态资源
class Users(AbstractUser):
    mobile = models.CharField(max_length=11,verbose_name='手机号')
    email_active = models.BooleanField(default=False,verbose_name='邮箱')
    avator = models.ImageField(
        upload_to='avator',verbose_name='头像',
        null=True,
        blank=True,
        )
    '''
        image = request.FILES.get('img')
        image.read() -> write(fp)
        avator = image // 图片验证 png jpeg webp 
        pip install pillow 
        avator/1.png md5(时间戳)->1.png->abc.png 
            a: 1.png == 1.png == 1_z234v5.png 
        avator.url
    '''
    # 头像本地化处理
    avator_url = models.URLField(
        null=True,
        blank=True,
        verbose_name='头像地址',
        )
    # 头像云端处理

    class Meta:
        db_table = 'users'
        # django原生ORM创建出来的表
        # users_users
        verbose_name_plural = '用户表'

# 平台
# 设备
 
class SocialUser(models.Model):
    '''
        社交表
    '''
    user = models.ForeignKey(Users,on_delete=models.CASCADE,verbose_name='用户')
    platfrom_type_choices = (
        (1,'PC'),
        (2,'Androia'),
        (3,'IOS'),
    )
    platfrom_ID = models.IntegerField(choices=platfrom_type_choices,verbose_name='平台类型')
    platfrom_choices = (
        (1,'QQ'),
        (2,'微博'),
        (3,'微信'),
    )
    platfrom_type = models.IntegerField(choices=platfrom_choices,verbose_name='社交平台')

    uid = models.CharField(max_length=100,verbose_name='用户ID')

    def __str__(self):
        return self.user.username

class Areas(models.Model):
    name = models.CharField(max_length=20,verbose_name='行政区名字')
    # area_id = models.IntegerField(
    #     verbose_name='行政区ID')
    parent = models.ForeignKey(
        to='self', # 自关联 这个表自己关联自己，自己是自己的外键,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='subs', # 反向查询时， p.areas_set.all() 当前省的所有市
    )


    def __str__(self):
        return self.name

    class Meta:
        db_table = 'areas'
        verbose_name_plural = '行政区'

'''
    765 
    utf-8 255 
    utf-8mb4 191
        max_length=
'''
class BaseModel(models.Model):
    # 模型类抽象基类
    create_time = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间',
    )
    update_time = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间',
    )

    class Meta:
        abstract = True


# 地址表
class Address(BaseModel,models.Model):
    user = models.ForeignKey(Users,on_delete=models.CASCADE,related_name='addres',verbose_name='用户')
    receiver = models.CharField(max_length=20,verbose_name='收件人')
    
    # models.PROTECT：删除关联数据,引发错误ProtectedError
    province = models.ForeignKey(Areas,on_delete=models.PROTECT,related_name='province_addres',verbose_name='省')
    city = models.ForeignKey(Areas,on_delete=models.PROTECT,related_name='city_addres',verbose_name='市')
    town = models.ForeignKey(Areas,on_delete=models.PROTECT,related_name='town_addres',verbose_name='区')
    place = models.CharField(max_length=50,verbose_name='地址')
    mobile = models.CharField(max_length=11,verbose_name='手机号')
    email = models.CharField(max_length=30,null=True,blank=True,default="",verbose_name='邮箱')
    is_delete = models.BooleanField(default=False,verbose_name='逻辑删除')
    default_address = models.IntegerField(default=0,verbose_name='默认地址')

    class Meta:
        db_table = 'address'
        verbose_name_plural = '用户地址表'
        ordering = ['-update_time']   # 默认的排序方式
